package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.entity.Person;
import com.example.domain.query.PersonQuery;
import com.example.mapper.PersonMapper;
import com.example.service.IPersonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2025-05-12
 */
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements IPersonService {

    @Override
    public IPage<Person> pageQuery(PersonQuery query, int pageNum, int pageSize) {
        Page<Person> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Person> wrapper = new QueryWrapper<>();

        if (query != null) {
            if (query.getPersonId() != null) {
                wrapper.eq("person_id", query.getPersonId());
            }
            if (StringUtils.hasText(query.getPersonName())) {
                wrapper.like("person_name", query.getPersonName());
            }
            if (StringUtils.hasText(query.getDepartment())) {
                wrapper.eq("department", query.getDepartment());
            }
            if (query.getHireDate() != null) {
                wrapper.apply("DATE(hire_date) = DATE({0})", query.getHireDate());
            }
            if (StringUtils.hasText(query.getEducation())) {
                wrapper.like("education", query.getEducation());
            }
            if (StringUtils.hasText(query.getMajor())) {
                wrapper.like("major", query.getMajor());
            }
            if (StringUtils.hasText(query.getTitle())) {
                wrapper.like("title", query.getTitle());
            }
            if (StringUtils.hasText(query.getPhone())) {
                wrapper.like("phone", query.getPhone());
            }
            if (StringUtils.hasText(query.getAddress())) {
                wrapper.like("address", query.getAddress());
            }
            if (StringUtils.hasText(query.getProfile())) {
                wrapper.like("profile", query.getProfile());
            }
        }
        return this.page(page, wrapper);
    }

    public Optional<Person> getById(int person_id) {
        return Optional.ofNullable(baseMapper.selectById(person_id));
    }
}
